package com.zeus.core.config;

import com.zeus.core.config.listener.ZeusAutoUpdateConfig;
import com.zeus.core.config.listener.ZeusAutoUpdateConfigApollo;
import com.zeus.core.config.listener.ZeusAutoUpdateConfigNacos;
import com.zeus.core.config.listener.ZeusMonitorContextClosedEventListener;
import com.zeus.core.config.spring.ZeusApolloEnableBean;
import com.zeus.core.config.spring.ZeusApplicationContextHelper;
import com.zeus.core.config.listener.ZeusMonitorContextStartEventListener;
import com.zeus.core.open.aspect.ZeusAlarmAspect;
import com.zeus.core.open.spi.ZeusMonitor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.ClassUtils;

import java.util.ServiceLoader;

/**
 * 监控Bean配置
 *
 * @author f1ybear_wang  2021/5/13 16:20
 */
@Configuration
public class ZeusMonitorAutoConfiguration {


    @Bean
    public ZeusApplicationContextHelper zeusApplicationContextHelper() {
        return new ZeusApplicationContextHelper();
    }


    @Bean
    public ZeusMonitorContextStartEventListener zeusMonitorApplicationListener() {
        return new ZeusMonitorContextStartEventListener();
    }

    @Bean
    public ZeusMonitorContextClosedEventListener zeusMonitorContextClosedEventListener() {
        return new ZeusMonitorContextClosedEventListener();
    }


    @Bean
    public ZeusAlarmAspect zeusAlarmAspect() {
        return new ZeusAlarmAspect();
    }



    /**
     * 宙斯Apollo开关注解
     *
     * @return  ZeusApolloEnableBean
     * @author f1ybear_wang  2022/5/7 15:39
     */
    @Bean
    public ZeusApolloEnableBean zeusApolloEnableBean() {
        try {
            return new ZeusApolloEnableBean();
        } catch (Throwable ignored) {
            // 忽略
        }
        return null;
    }


    @Bean
    public ZeusAutoUpdateConfig zeusAutoUpdateConfigApollo() {

        if (ClassUtils.isPresent("com.ctrip.framework.apollo.ConfigService",
                this.getClass().getClassLoader())) {
            return new ZeusAutoUpdateConfigApollo();
        }
        if (ClassUtils.isPresent("com.alibaba.nacos.api.config.ConfigService",
                this.getClass().getClassLoader())) {
            return new ZeusAutoUpdateConfigNacos();
        }
        return null;
    }
}
